if(typeof(own) === 'undefined'){
    var own = {};
}

/**
 * 并行下载js与css，选择是否顺序执行
 *
 * @param urls
 * @param fun
 * @param isSequence 是否顺序执行 默认顺序执行，false 不并行执行
 */
own.request = function(urls,fun,isSequence){
    var loadContent = {};
    /**
     * 需要使用ajax加载js的次数
     * @type {number}
     */
    var loadjsCnt = 0;
    var num = 0;
    var frag = document.createDocumentFragment();
    if(typeof urls == 'string'){
        var urls_ = urls.split(',');
        var cnt = urls_.length;
        //加载次数
        loadJsCnt_(urls_);

        for(var i=0;i<cnt;i++){
            _load(urls_,i);
        }
    };
    document.head.appendChild(frag);
    frag = null;

    /**
     * 获取需要使用ajax方式加载的js次数
     * @param urls
     * @param i
     * @private
     */
    function loadJsCnt_(urls_){
        for(var i=0,cnt = urls_.length;i<cnt;i++) {
            var url = urls_[i];
            if (typeof(own.util) == 'object') {
                url = own.util.trim(urls_[i]);
            }
            if (typeof(own.config) == 'object' && own.config.modules.hasOwnProperty(url)) {
                var mod = own.config.modules[url];
                if(typeof(mod) == 'string'){
                    url = mod;
                }else if(typeof(mod) == 'object'){
                    url = mod.js;
                }
            }
            if (url.indexOf('http') != 0 && url.lastIndexOf('.js') != -1) {
                loadjsCnt++;
            }
        }
    }

    /**
     * 进行加载
     * @param urls_
     * @param i
     * @private
     */
    function _load(urls_,i){
        var url = urls_[i];
        var ishttp = false;
        var module = '';
        if(typeof(own.util) == 'object'){
            url = own.util.trim(urls_[i]);
        }
        module = url;

        if(typeof(own.config) == 'object' && own.config.modules.hasOwnProperty(url)){
            // url = own.config.modules[url];
            var mod = own.config.modules[url];
            if(typeof(mod) == 'string'){
                url = mod;
            }else if(typeof(mod) == 'object'){
                if(mod.js&&mod.css){
                    url = mod.js;
                    var urls_ = [mod.css];
                    _load(urls_,0);
                }else if(!mod.js&&mod.css){
                    url = mod.css;
                }else if(mod.js){
                    url = mod.js;
                }
            }
        }

        if(url.indexOf('http') != 0){
            url = own.config.localhostPath + url;
        }else{
            ishttp = true;
        }
        var obj = null;
        if(url.lastIndexOf('.js') != -1) {
            if(ishttp){
                var obj = document.createElement('script');
                obj.type = 'text/javascript';
                if(typeof(own.util) == 'object'){
                    obj.id = own.util.hashCode(url);
                }else{
                    obj.id = url;
                }
                obj.charset = 'UTF-8';
                obj.src = url;
            }else{
                _load_(url,0,module);
            }
        }else if(url.lastIndexOf('.css') != -1){
            var obj = document.createElement('link');
            obj.rel = 'stylesheet';
            if(typeof(own.util) == 'object'){
                obj.id = own.util.hashCode(url);
            }else{
                obj.id = url;
            }
            obj.charset = 'UTF-8';
            obj.href = url;
        }

        if(obj != null){
            if(url.lastIndexOf('.css') != -1) {
                // console.log('开始加载',obj);
                obj.onload = obj.onreadystatechange = function (e) {
                    // console.log('加载完成',obj);
                    if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") {
                        num++;
                        if (num == cnt) {
                            typeof(fun) == 'function' ? fun() : null;
                            obj.onload = obj.onreadystatechange = null;
                        }
                    }
                }
                obj.onerror = function () {
                    console.error('异步加载js错误：' + url);
                    obj.onerror = null;
                }
            }
            frag.appendChild(obj);
        }
    }

    function _load_(url,num,module){
        if(num >= own.config.retry){//超过最大重试次数
            return;
        }
        $$.util.ajax({
            url:url,
            method:'GET',
            contentType:'application/x-javascript',
            dataType:'text',
            fun:function(a,b){
                if(a){
                    var script = document.createElement('script');
                    script.innerHTML = b;
                    loadContent[module] = script;
                    if(loadjsCnt == Object.keys(loadContent).length){//全部加载完成
                        _loadJs();
                    }
                }else{
                    num ++;
                    _load_(url,num);
                }
            }
        });
    }

    function _loadJs(){
        var urls_ = urls.split(',');
        for(var i=0,cnt = urls_.length;i<cnt;i++){
            var url = module = urls_[i];
            if(typeof(own.util) == 'object'){
                url = own.util.trim(urls_[i]);
            }
            if(typeof(own.config) == 'object' && own.config.modules.hasOwnProperty(url)){
                var mod = own.config.modules[url];
                if(typeof(mod) == 'string'){
                    url = mod;
                }else if(typeof(mod) == 'object'){
                    url = mod.js;
                }
            }
            if(url.indexOf('http') != 0 && url.lastIndexOf('.js') != -1){
                document.head.appendChild(loadContent[module]);
            }
        }
        loadContent = null;
        if(typeof(fun) == 'function'){
            fun();
        }else{
            eval(fun);
        }
    }
};
